/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QCoreApplication>

#include "trackerserver.h"

#include "bencoder.h"
#include "bdecoder.h"
#include "exceptions.h"
#include "exceptionhandler.h"

#include "globals.h"
#include "log.h"

#include "torrentmanager.h"

#include "mainwindow.h"

#include <QtCore>
#include <QMessageBox>

void myMessageOutput(QtMsgType type, const char *msg)
{
	switch (type)
	{
		case QtDebugMsg:
			Globals::instance().log()->logMessage(msg);
			break;
		case QtWarningMsg:
			fprintf(stderr, "Warning: %s\n", msg);
			break;
		case QtCriticalMsg:
			fprintf(stderr, "Critical: %s\n", msg);
			break;
		case QtFatalMsg:
			fprintf(stderr, "Fatal: %s\n", msg);
			abort();
	}
}

int main(int argc, char *argv[])
{
	qInstallMsgHandler(myMessageOutput);
	QApplication app(argc, argv);
	
	try
	{
		Globals& gl = Globals::instance();
	}
	catch (DBException& ex)	
	{
		QMessageBox::critical(0l, "Database error", ex.getMessage());
		Globals::Destroy();
		
		return -1;
	}
	
	Globals& g = Globals::instance();

	if (!g.server())
	{
		Globals::Destroy();
		return -1;
	}

	g.server()->start();

	MainWindow mw;
	mw.show();

	return app.exec();
}
